package com.hxkj.controller;

import com.hxkj.constant.Constants;
import com.hxkj.domain.enums.UserExceptionEnum;
import com.hxkj.domain.po.SysUser;
import com.hxkj.domain.result.UserParameterDataStruct;
import com.hxkj.service.UserService;
import com.hxkj.token.TokenGenerator;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.service.sftp.domain.annotation.FormatUrl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * created by kezhijie on 2018/8/24 10:50.
 * email to kezhijie@hxoss.com
 */
@RestController
@RequestMapping("userinfo")
public class UserInfoController {

    @Autowired
    private UserService userService;

    @Autowired
    private TokenGenerator tokenGenerator;
    @Autowired
    private RedisTemplate redisTemplate;

    @Value("${storage.imgUrl}")
    private String imgUrl;

    @GetMapping(value = "get")
    public Callable<ResultDataStruct<SysUser>> get(@RequestParam("token") String token) throws Exception {
        return () -> {
            ResultDataStruct<SysUser> resultDataStruct = new ResultDataStruct<>();
            try {
                String userId = tokenGenerator.getUserIdByToken(token);
                SysUser sysUser = userService.findByUserId(userId);

                resultDataStruct.setCode(200);
                resultDataStruct.setMessage("成功");
                resultDataStruct.setData(sysUser);
            } catch (Exception e) {
                resultDataStruct.setCode(500);
                resultDataStruct.setMessage(e.getMessage());
            }
            return resultDataStruct;
        };
    }

    @PostMapping(value = "update")
    @FormatUrl
    public Callable<ResultDataStruct<SysUser>> update(@RequestBody SysUser sysUser) throws Exception {
        return () -> userService.update(sysUser);
    }

    @PostMapping(value = "updatePhone")
    public Callable<ResultMessageStruct> updatePhone(@RequestBody UserParameterDataStruct<SysUser, String> userParameterDataStruct) throws Exception {
        return () -> {
            ResultMessageStruct resultMessageStruct = new ResultMessageStruct();

            try {

                SysUser sysUser = userParameterDataStruct.getData();

                String storeKey = Constants.REDIS_PREFIX_TELEPHONE_LOGIN + sysUser.getPhone();
                if (Objects.isNull(redisTemplate.boundValueOps(storeKey).get()))
                    throw new CustomException(UserExceptionEnum.VALIDATECODE_INVALID);

                String originCode = redisTemplate.boundValueOps(storeKey).get().toString();
                if (!originCode.equalsIgnoreCase(userParameterDataStruct.getCode()))
                    throw new CustomException(UserExceptionEnum.VALIDATECODE_IS_ERROR);

                // 移除验证码
//                redisTemplate.delete(storeKey);

                resultMessageStruct = userService.updatePhone(sysUser);

            } catch (CustomException e) {
                resultMessageStruct.setCode(e.getCode().getCode());
                resultMessageStruct.setMessage(e.getMessage());
            } catch (Exception e) {
                resultMessageStruct.setCode(500);
                resultMessageStruct.setMessage(e.getMessage());
            }
            return resultMessageStruct;
        };
    }
}
